package com.szty.libarytest.other

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.FileUtils
import com.szty.libarytest.R
import com.szty.libarytest.longConnect.DotBean
import com.szty.libarytest.longConnect.manager.ClientConnectManager
import com.szty.libarytest.longConnect.manager.SendPointService
import kotlinx.android.synthetic.main.activity_long_connect.*
import java.io.File

class LongConnectActivity : AppCompatActivity(){

    private var mService: SendPointService.SendBind? = null
    private val mServiceConnection by lazy {
        object : ServiceConnection {
            override fun onServiceDisconnected(name: ComponentName?) {}
            override fun onServiceConnected(name: ComponentName, service: IBinder) {
                if (service is SendPointService.SendBind) {
                    mService = service
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if(mService != null){
            unbindService(mServiceConnection)

        }else{
            "服务未绑定".loge()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_long_connect)
        FileUtils.isDir(File("/data/0/1"))
        ClientConnectManager.getInstance().init(this)
        btn_send_message.setOnClickListener {
            mService?.sendPointToService(DotBean())
        }
        btn_connect_service.setOnClickListener {
            ClientConnectManager.getInstance().connect(this)
        }
        btn_discount_service.setOnClickListener {
            ClientConnectManager.getInstance().disConnect()
        }
        btn_check_service.setOnClickListener {
            ClientConnectManager.getInstance().checkSocketStatus()
        }
        btn_bind_service.setOnClickListener {
            bindService(
                Intent(this, SendPointService::class.java),
                mServiceConnection,
                Context.BIND_AUTO_CREATE
            )
        }
    }

}